//
//  SettingChangeController.m
//  QuickCure1
//
//  Created by zhouxuanhe on 16/11/1.
//  Copyright © 2016年 maoge. All rights reserved.
//

#import "SettingChangeController.h"
#import "SVProgressHUD.h" 

@interface SettingChangeController ()
@property (weak, nonatomic) IBOutlet UITextField *txf_oldPassword;

@property (weak, nonatomic) IBOutlet UITextField *txf_newPasswordFirst;

@property (weak, nonatomic) IBOutlet UITextField *txf_newPasswordSecond;

@property (weak, nonatomic) IBOutlet UIButton *btn_modify;

@end

@implementation SettingChangeController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI];
}


#pragma mark-忘记密码界面
//- (void)setIsForget:(BOOL)isForget{
//    _isForget = isForget;
//
//    
//}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (self.isForget) {
        [self.btn_modify setTitle:@"重置密码" forState:UIControlStateNormal];
        self.txf_oldPassword.placeholder = @"请输入账号";
        self.title = @"忘记密码";
    }else{
        [self.btn_modify setTitle:@"修改密码" forState:UIControlStateNormal];
        self.txf_oldPassword.placeholder = @"请输入密码";
        self.title = @"修改密码";
    }

    
}


#pragma mark -
#pragma mark - 初始化界面
-(void)setupUI
{
    
    self.title = @"修改密码";
    [_txf_newPasswordSecond setSecureTextEntry:YES];
}

#pragma mark -
#pragma mark - 修改密码
- (IBAction)buttonModifyClick:(UIButton *)sender {
    
    
    [self modifyPassword:[MLDefaults valueForKey:kNowPasswordKey]];
}
//修改密码
-(void)modifyPassword:(NSString *)password
{
    
    
    //忘记密码
    if (self.isForget) {
        if ([_txf_newPasswordFirst.text isEqualToString:_txf_newPasswordSecond.text]) {
            [self modifySuccess];
        }else{
            [SVProgressHUD showErrorWithStatus:@"你输入的账号或密码有误,请重新输入"];
        }
        
    }else{
        //修改密码
        if ([password isEqualToString:_txf_oldPassword.text] && [_txf_newPasswordFirst.text isEqualToString:_txf_newPasswordSecond.text]) {
            [self modifySuccess];
            
        }else{
            [SVProgressHUD showErrorWithStatus:@"你输入的账号或密码有误,请重新输入"];
        }
    }
    

    
    

}

//修改密码成功
- (void)modifySuccess
{
    //修改密码成功通知
    //[[NSNotificationCenter defaultCenter] postNotificationName:kIsUserModifyKey object:nil];
    
    [MLDefaults setValue:_txf_newPasswordFirst.text forKey:kNowPasswordKey];
    //同步
    [MLDefaults synchronize];
    
    
    [SVProgressHUD showSuccessWithStatus:@"修改成功,请重新登录!"];
    
    //显示左边视图
    JASidePanelController *sideVc = (JASidePanelController*)[UIApplication sharedApplication].keyWindow.rootViewController;
    [sideVc showLeftPanelAnimated:YES];
    [self.navigationController popToRootViewControllerAnimated:YES];
    //注销
    [[NSNotificationCenter defaultCenter] postNotificationName:kIsUserQuitKey object:nil];

    
    
}
@end
